//
//  CSBEnterInSweeping.swift
//  CurSodoboAer
//
//  Created by CurSodoboAer on 2024/11/4.
//

import UIKit
import JDStatusBarNotification
class CSBEnterInSweeping: UIViewController {
    var csbColoter:UIColor = .brown
    var csbFplowe:CGFloat = 23.1
    var csbHeihei:UIFont?
    @IBOutlet weak var csbEnterViewemail: UITextField!
    
    
    @IBOutlet weak var csbEnterViewPswl: UITextField!
    
    
    @IBOutlet weak var csbQungStatus: UIButton!
    
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        var csbBayui = [Int]()
        let csbNUmber = Array(repeating: 10, count: 64)
        let csbNumbe2 = Array(repeating: 96, count: 50)
        var cabSunmAll: Int = 0

        csbBayui.append(11)
        cabSunmAll = csbNUmber.count
        if cabSunmAll > 5 {
            csbBayui.append(contentsOf: csbNUmber)
        }else{
            csbBayui.append(contentsOf: csbNumbe2)
        }

        
        if AppDelegate.comeComeCSBELUA == false && AppDelegate.comeOKAUCSBELUA == false{
            
            csbBayui.append(contentsOf: [52, 6, 11])
            csbBayui.removeLast()
            cabSunmAll = csbBayui.count

          
           
            let showingCSB = CSBELUASweeping.init()
            showingCSB.myAgreenClosure = {
                self.csbQungStatus.isSelected = true
            }
            if (csbBayui.count >= 0) {
                showingCSB.modalPresentationStyle = .fullScreen
                self.present(CSBONabviSweeping.init(rootViewController: showingCSB) , animated: true)
                AppDelegate.comeComeCSBELUA = true
            }
           
        }
        
        csbQungStatus.isSelected = AppDelegate.comeOKAUCSBELUA
    }
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        csbEnterViewPswl.leftViewMode = .always
        csbEnterViewemail.leftViewMode = .always
        
        let emailLest = UIImageView(image: UIImage.init(named: "csb_enter_eamil"))
        let pswlLest = UIImageView(image: UIImage.init(named: "csb_enter_psw"))
        emailLest.frame = CGRect(x: 15, y: 12, width: 20, height: 20)
        pswlLest.frame = CGRect(x: 15, y: 12, width:20, height: 20)
        
        var csbBayui = [Int]()
        let csbNUmber = Array(repeating: 10, count: 64)
        let csbNumbe2 = Array(repeating: 96, count: 50)
        var cabSunmAll: Int = 0

        csbBayui.append(11)
        cabSunmAll = csbNUmber.count
        if cabSunmAll > 5 {
            csbBayui.append(contentsOf: csbNUmber)
        }else{
            csbBayui.append(contentsOf: csbNumbe2)
        }

        csbBayui.append(contentsOf: [52, 6, 11])
        csbBayui.removeLast()
        cabSunmAll = csbBayui.count

        if (cabSunmAll >= 0) {
            csbEnterViewemail.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 50, height: 48))
            csbEnterViewPswl.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 50, height: 48))
            
            csbEnterViewemail.layer.cornerRadius = 12
        }

        if (csbBayui.count >= 0) {
            csbEnterViewemail.addSubview(emailLest)
            csbEnterViewPswl.addSubview(pswlLest)
            csbEnterViewPswl.layer.cornerRadius = 12
        }
       

       
    }


    @IBAction func csbClikfGono(_ sender: UIButton) {
        if AppDelegate.comeOKAUCSBELUA == false {
            
            NotificationPresenter.shared.present("Please read and agree to the terms and conditions first.", includedStyle: .warning)
            
            return
        }
        
        
        guard let emiajkh = csbEnterViewemail.text,
        let psdjkh = csbEnterViewPswl.text,
        emiajkh.count != 0,
        psdjkh.count != 0 else {
            NotificationPresenter.shared.present("Email address or password can not be empty", includedStyle: .warning)
  
            return
        }
        
        
        NotificationPresenter.shared.present("")
        NotificationPresenter.shared.displayActivityIndicator(true)
       
        
        if emiajkh == "Qin1234@gmail.com" && psdjkh == "12345678" && (UserDefaults.standard.object(forKey: "testuseraccountDel") as? Bool ?? false)  == false{
           
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1.5, execute: DispatchWorkItem(block: {
                NotificationPresenter.shared.dismiss()
                NotificationPresenter.shared.present("Log in successful!", includedStyle: .success)
                CSBFetcherAll.Hammer.ceaterTestUserCSBINFO()
                CSBFetcherAll.Hammer.ifLogStatusCSB = true
               
                ((UIApplication.shared.delegate) as? AppDelegate)?.window?.rootViewController =  CSBBarSweeping.init()
                return
            }))
            

        }else{
            let emailRegex = "[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}"
                   
            let emailPredicate = NSPredicate(format:"SELF MATCHES %@", emailRegex)
            let emaiFormate = emailPredicate.evaluate(with: emiajkh)
            if emaiFormate == false {
                NotificationPresenter.shared.present("The email format you entered is incorrect", includedStyle: .warning)
            }else{
                DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1.5, execute: DispatchWorkItem(block: {
                    NotificationPresenter.shared.dismiss()
                    CSBFetcherAll.Hammer.ifLogStatusCSB = true
                    NotificationPresenter.shared.present("Log in successful!", includedStyle: .success)
                    CSBFetcherAll.Hammer.melingCSBINfo["csbEmail"] = emiajkh
                    CSBFetcherAll.Hammer.melingCSBINfo["csbpersonID"] = "\(Int.random(in: 50000...60000))"
                    CSBFetcherAll.Hammer.melingCSBINfo["csbpersonName"] = "NULL"
                    
//                    CSBFetcherAll.Hammer.melingCSBINfo["csbpersonHead"] = "userHear9"
//                    CSBFetcherAll.Hammer.shoHeadCSB = UIImage(named: "userHear9")
                    
                    
                    ((UIApplication.shared.delegate) as? AppDelegate)?.window?.rootViewController =  CSBBarSweeping.init()
                  
                }))
                
            }
            
        }
        
        
    }
    
    
    @IBAction func sdbGoGuestClick(_ sender: UIButton) {
        var ikol = ["csbFplowe":csbFplowe]
        if self.view.backgroundColor == .brown {
            ikol["allElseTigerCSB"]  = 77.8
            csbFplowe += 1.1
        }else{
            csbFplowe += 2.1
        }

        if csbFplowe < 10{
            return
        }
        if AppDelegate.comeOKAUCSBELUA == false {
            
            NotificationPresenter.shared.present("Please read and agree to the terms and conditions first.", includedStyle: .warning)
            
            return
        }
        
        NotificationPresenter.shared.present("")
        NotificationPresenter.shared.displayActivityIndicator(true)
        
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1.5, execute: DispatchWorkItem(block: {
            NotificationPresenter.shared.dismiss()
            NotificationPresenter.shared.present("Welcome!", includedStyle: .success)
           
            CSBFetcherAll.Hammer.ifLogStatusCSB = true
            
            CSBFetcherAll.Hammer.melingCSBINfo["csbEmail"] = "78645509@gmail.com"
            CSBFetcherAll.Hammer.melingCSBINfo["csbpersonID"] = "\(Int.random(in: 50000...60000))"
            CSBFetcherAll.Hammer.melingCSBINfo["csbpersonHead"] = "userHear9"
            CSBFetcherAll.Hammer.shoHeadCSB = UIImage(named: "userHear9")
            CSBFetcherAll.Hammer.melingCSBINfo["csbpersonName"] = "Lily"
            
            
            ((UIApplication.shared.delegate) as? AppDelegate)?.window?.rootViewController =  CSBBarSweeping.init()
            return
        }))
        
        
    }
    
    
    @IBAction func csbQuanClivkg(_ sender: UIButton) {
        var csbBayui = [Int]()
        let csbNUmber = Array(repeating: 10, count: 64)
        let csbNumbe2 = Array(repeating: 96, count: 50)
        var cabSunmAll: Int = 0

        csbBayui.append(11)
        cabSunmAll = csbNUmber.count
        if cabSunmAll > 5 {
            csbBayui.append(contentsOf: csbNUmber)
        }else{
            csbBayui.append(contentsOf: csbNumbe2)
        }

        csbBayui.append(contentsOf: [52, 6, 11])
        csbBayui.removeLast()
        cabSunmAll = csbBayui.count

        if (cabSunmAll >= 0) {
            sender.isSelected = !sender.isSelected
            AppDelegate.comeOKAUCSBELUA = sender.isSelected
        }

        
       
    }
}
